package com.ruoyi.activiti.mapper;

import com.ruoyi.activiti.domain.MyHistoricTask;
import com.ruoyi.activiti.domain.MyProcessInstanceCondition;
import com.ruoyi.activiti.domain.TaskCondition;
import com.ruoyi.activiti.domain.TaskVo;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

public interface TaskMapper {

    @Select("<script>" +
            " select type_ action, message_ comment" +
            " from act_hi_comment where task_id_=#{taskId}" +
            "</script>")
    List<MyHistoricTask> queryComments(@Param("taskId") String taskId);

    @Select("<script>" +
            " select actTask.task_def_key_ taskDefinitionKey, actTask.id_ taskId, actTask.name_ taskName" +
            " , actTask.create_time_ createTime, actProcInst.start_time_ startTime" +
            " , actProcInst.id_ instanceId, actProcInst.name_ instanceName" +
            " , actProcDef.id_ definitionId, actProcDef.name_ definitionName" +
            " , actTask.suspension_state_ suspendState, actTask.owner_ userId, actTask.assignee_ assignee" +
            " , su.nick_name creatorName " +
            " from act_ru_task actTask " +
            " left join act_hi_procinst actProcInst on actTask.proc_inst_id_=actProcInst.id_" +
            " left join act_re_procdef actProcDef on actTask.proc_def_id_=actProcDef.id_" +
            " left join sys_user su on su.user_name=actProcInst.START_USER_ID_ " +
            " where 1=1" +
            " <if test='taskId != null and taskId.length()>0'> and actTask.id_=#{taskId}</if>" +
            " <if test='instanceId != null and instanceId.length()>0'> and actTask.proc_inst_id_=#{instanceId}</if>" +
            " <if test='definitionId != null and definitionId.length()>0'> and actTask.proc_def_id_=#{definitionId}</if>" +
            " <if test='username != null and username.length()>0'>" +
            " and (" +
            " (actTask.ASSIGNEE_=#{username})" + //处理人为自己
            " or (actTask.ASSIGNEE_ is null and #{username} in (select user_id_ from ACT_RU_IDENTITYLINK where task_id_=actTask.id_))" +//候选人为自己
            " or (actTask.ASSIGNEE_ is null and #{username} in (select USER_ID_ from ACT_ID_MEMBERSHIP where GROUP_ID_ in (select GROUP_ID_ from ACT_RU_IDENTITYLINK where task_id_=actTask.id_))" +//勾选组为自己
            " ) " +
            " )</if>" +
            " <if test='taskName != null and taskName.length()>0'> and actTask.name_ like concat('%',#{taskName},'%')</if>" +
            " <if test='taskNameIn != null and taskNameIn.size()>0'>" +
            "   and actTask.name_ in" +
            "   <foreach collection='taskNameIn' item='item' index='index' separator=',' open='(' close=')'>" +
            "       #{item}" +
            "   </foreach>" +
            " </if>" +
            " order by actTask.create_time_ desc" +
            "</script>")
    List<TaskVo> taskList(TaskCondition taskCondition);

    @Select("<script>" +
            " select distinct r.role_id roleId, r.role_name roleName, r.role_key roleKey" +
            " , r.role_sort roleSort, r.data_scope dataScope, r.menu_check_strictly menuCheckStrictly, r.dept_check_strictly deptCheckStrictly" +
            " , r.status status, r.del_flag delFlag, r.create_time createTime, r.remark" +
            " from sys_role r " +
            " where r.role_key in (select group_id_ from ACT_RU_IDENTITYLINK idlink where task_id_=#{taskId})" +
            " </script>")
    Set<SysRole> queryTaskCandidateGroups(@Param("taskId") String taskId);

    @Select("<script>" +
            " select u.user_id userId, u.dept_id deptId, u.user_name userName , u.nick_name nickName" +
            " , u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag delFlag" +
            " , u.login_ip loginIp, u.login_date loginDate, u.create_by createBy, u.create_time createTime, u.remark" +
            " from sys_user u " +
            " where u.user_name in (select user_id_ from ACT_RU_IDENTITYLINK idlink where task_id_=#{taskId})" +
            " </script>")
    Set<SysUser> queryTaskCandidateUsers(@Param("taskId") String taskId);

    @Select("<script>" +
            " select procInst.id_" +
            " from act_hi_procinst procInst" +
            " <where>" +
            " <if test='processInstanceId != null and processInstanceId.length()>0'> and procInst.id_=#{processInstanceId}</if>" +
            " <if test='businessKey != null and businessKey.length()>0'> and procInst.business_key_=#{businessKey}</if>" +
            " <if test='processDefinitionId != null and processDefinitionId.length()>0'> and procInst.proc_def_id_=#{processDefinitionId}</if>" +
            " <if test='username != null and username.length()>0'>" +
            " and exists(select 1 from act_hi_taskinst taskInst" +
            " where taskInst.end_time_ is not null and taskInst.proc_inst_id_=procInst.id_ and taskInst.assignee_=#{username} )" +
            " </if>" +
            " <if test='startUserName != null and startUserName.length()>0'> and taskInst.start_user_id_=#{startUserName}</if>" +
            " </where>" +
            "</script>")
    Set<String> instanceIds(MyProcessInstanceCondition condition);
}
